﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using E_Commerce.Domain.Entities;
using E_Commerce.Domain.Interfaces;
using E_Commerce.Domain.Dto;
using Dapper;
using Microsoft.Extensions.Configuration;//引用

namespace E_Commerce.Infrastructure.Data
{
    public class SeckillRepository : DapperRepository<Seckill>, ISeckillRepository
    {
        //依赖注入
        private readonly IConfiguration _configuration;
        public SeckillRepository(IConfiguration configuration)
        {
            _configuration = configuration;
            //连接字符串
            this.ConnStr = _configuration.GetConnectionString("ConnStr");
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
            Conn = new MySql.Data.MySqlClient.MySqlConnection(this.ConnStr);
        }
        /// <summary>
        /// 添加秒杀订单
        /// </summary>
        /// <param name="orders"></param>
        /// <returns></returns>
        public async Task<int> AddOrderAsync(Orders orders)
        {
            try
            {
                return (int)await Conn.InsertAsync<Orders>(orders);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }
        /// <summary>
        /// 添加秒杀商品
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public async Task<int> AddSeckillAsync(Seckill s)
        {
            try
            {
                return (int)await Conn.InsertAsync(s);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }
        /// <summary>
        /// 秒杀商品详情
        /// </summary>
        /// <param name="productId">商品ID</param>
        /// <returns></returns>
        public SeckillDto FillSeckill(int productId)
        {
            string sql = $"select * from Product p join Seckill s on p.ProId=s.ProductId where s.ProductId={productId}";
            return Conn.Query<SeckillDto>(sql).FirstOrDefault();
        }

        /// <summary>
        /// 显示秒杀商品
        /// </summary>
        /// <returns></returns>
        public List<SeckillDto> GetSeckill(int pageIndex, int pageSize, out int totalCount) 
        {
            string sql = "select * from Product p right join Seckill s on p.ProId=s.ProductId left join SeckOrder o on s.SeckId=o.SeckId left join Orders r on o.OrderId=r.OrderId";
            string sqlCount = $"select count(*) from Product p right join Seckill s on p.ProId=s.ProductId left join SeckOrder o on s.SeckId=o.SeckId left join Orders r on o.OrderId=r.OrderId";//总条数
            sql += $" limit @a1,@a2";
            totalCount = Conn.ExecuteScalar<int>(sqlCount);
            return Conn.Query<SeckillDto>(sql,new { a1=(pageIndex-1)*pageSize,a2=pageSize}).ToList();
        }
        /// <summary>
        /// 修改秒杀商品状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> UptSeckillAsync(int id)
        {
            try
            {
                string sql = $"update Seckill set SeckStatic=1 where SeckId={id}";
                return await Conn.ExecuteAsync(sql);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
           
        }

    }
}
